<template>
  <div id="loginPage">
    <div class="login-container">
      <div class="login-header">
        <h2>用户登录</h2>
      </div>

      <a-form
        :model="formState"
        name="basic"
        :label-col="{ span: 6 }"
        :wrapper-col="{ span: 18 }"
        autocomplete="off"
        @finish="onFinish"
        @finishFailed="onFinishFailed"
        class="login-form"
      >
        <a-form-item
          label="账号"
          name="userAccount"
          :rules="[{ required: true, message: '请输入账号！' }]"
        >
          <a-input
            v-model:value="formState.userAccount"
            placeholder="请输入用户名或邮箱"
            size="large"
            aria-label="用户账号输入框"
            :class="{ 'input-error': errors.userAccount }"
          />
        </a-form-item>
        <div v-if="errors.userAccount" class="error-message">
          {{ errors.userAccount }}
        </div>
        <a-form-item
          label="密码"
          name="userPassword"
          :rules="[{ required: true, message: '请输入密码！' }]"
        >
          <a-input-password
            v-model:value="formState.userPassword"
            placeholder="请输入密码"
            size="large"
            aria-label="密码输入框"
            :class="{ 'input-error': errors.userPassword }"
          />
        </a-form-item>
        <div v-if="errors.userPassword" class="error-message">
          {{ errors.userPassword }}
        </div>
        <a-form-item :wrapper-col="{ offset: 6, span: 18 }">
          <a-button
            type="primary"
            html-type="submit"
            :loading="loading"
            block
            size="large"
            aria-label="登录"
          >
            登录
          </a-button>
        </a-form-item>
      </a-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { message } from "ant-design-vue";
import { userLogin } from "@/api/user";
import { useRouter } from "vue-router";
import { currentUserStore } from "@/store/userLoginStore";

interface FormState {
  userAccount: string;
  userPassword: string;
}

const formState = reactive<FormState>({
  userAccount: "",
  userPassword: "",
});

// 错误信息
const errors = reactive({
  userAccount: "",
  userPassword: "",
});

// 登录状态
const isLoggingIn = ref(false);
const router = useRouter();

// 表单验证
const validateForm = () => {
  let isValid = true;

  // 重置错误信息
  errors.userAccount = "";
  errors.userPassword = "";

  // 验证账号
  if (!formState.userAccount.trim()) {
    errors.userAccount = "请输入用户账号";
    isValid = false;
  } else if (formState.userAccount.length < 4) {
    errors.userAccount = "账号长度不能少于4位";
    isValid = false;
  }

  // 验证密码
  if (!formState.userPassword) {
    errors.userPassword = "请输入密码";
    isValid = false;
  } else if (formState.userPassword.length < 8) {
    errors.userPassword = "密码长度不能少于8位";
    isValid = false;
  }

  return isValid;
};

const loginUserStore = currentUserStore();

const onFinish = async () => {
  // 表单验证
  if (!validateForm()) {
    return;
  }

  try {
    isLoggingIn.value = true;
    // 调用登录API
    const res: any = await userLogin({
      userAccount: formState.userAccount,
      userPassword: formState.userPassword,
    });
    // 登录成功
    if (res.data?.code === 0) {
      // 跳转到首页或其他页面
      await loginUserStore.fetchLoginUser();
      message.success("登录成功！");
      await router.push({
        path: "/",
        // 不保存上一页
        replace: true,
      });
    } else {
      message.error("登录失败," + res.data?.description);
    }
  } catch (error: any) {
    message.error("登录失败，请稍后重试");
  } finally {
    isLoggingIn.value = false;
  }
};

const onFinishFailed = () => {
  message.error("请检查输入信息");
};
</script>

<style scoped>
#loginPage {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.login-container {
  width: 100%;
  max-width: 450px;
  background: white;
  border-radius: 10px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.login-header {
  background: #409eff;
  padding: 30px 20px;
  text-align: center;
}

.login-header h2 {
  color: white;
  margin: 0;
  font-size: 24px;
  font-weight: 500;
}

.login-form {
  padding: 30px;
}

:deep(.ant-form-item-label label) {
  font-weight: 500;
  color: #555;
}
</style>
